package com.liwentao.seckill.entity.vo;

import com.liwentao.seckill.util.ValidatorUtil;
import com.liwentao.seckill.validator.IsMobile;
import com.liwentao.seckill.validator.IsPassword;
import org.thymeleaf.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsPasswordValidator implements ConstraintValidator<IsPassword,String> {
    private boolean required=false;

    @Override
    public void initialize(IsPassword constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
            return ValidatorUtil.isPassword(s);
        }else {
            if (StringUtils.isEmpty(s)){
                return true;
            }else {
                return ValidatorUtil.isPassword(s);
            }
        }
    }
}
